<template>
  <transition name="el-zoom-in-center">
    <div class="JADP-preview-main">
      <div class="JADP-common-page-header">
        <el-page-header
          @back="goBack"
          :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
        />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail"
            >确 定</el-button
          >
          <el-button @click="goBack">取 消</el-button>
        </div>
      </div>
      <el-row
        :gutter="15"
        class=" main"
        :style="{ margin: '0 auto', width: '100%' }"
      >
        <el-form
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="small"
          label-width="100px"
          label-position="right"
          :disabled="!!isDetail"
        >
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input
                v-model="dataForm.title"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="封面图片" prop="picture">
              <JADP-UploadImg
                v-model="pictureList"
                :fileSize="2"
                sizeUnit="MB"
                :limit="9"
              >
              </JADP-UploadImg>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="内容" prop="content">
              <JADP-Quill
                v-model="dataForm.content"
                placeholder="请输入内容..."
              >
              </JADP-Quill>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="是否发布" prop="realse">
              <el-switch v-model="dataForm.realse" active-value="true">
              </el-switch>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="是否置顶" prop="topset">
              <el-switch v-model="dataForm.topset" active-value="true">
              </el-switch>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="发布时间" prop="realsetime">
              <el-date-picker
                v-model="dataForm.realsetime"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                type="datetime"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="timestamp"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { previewDataInterface } from "@/api/systemData/dataInterface";
import ExportBox from "./ExportBox";
export default {
  name: "news",
  components: { ExportBox },
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      activeName: "",
      dataForm: {
        title: undefined,
        picture: [],
        content: undefined,
        realse: 1,
        topset: 1,
        realsetime: undefined,
        readercount: undefined,
        creatoruserid: "",
        creatortime: "",
        lastmodifyuserid: "",
        lastmodifytime: "",
        organizeId:""
      },
      rules: {
        title: [
          {
            required: true,
            message: "请输入",
            trigger: "blur"
          }
        ],
        picture: [
          {
            required: true,
            message: "请上传图片",
            trigger: "blur"
          }
        ]
      },
      pictureList: [],
      picture: []
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    goBack() {
      this.$emit("refresh");
    },
    init(id, isDetail) {
      this.pictureList = [];
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/news/Xy_ks_news/" + this.dataForm.id,
            method: "get"
          }).then(res => {
            this.dataForm = res.data;
            this.pictureList = this.dataForm.picture
              ? JSON.parse(this.dataForm.picture)
              : [];
            this.loading = false;
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$set(
        this.dataForm,
        "picture",
        this.pictureList.length ? JSON.stringify(this.pictureList) : ""
      );

      this.$refs["elForm"].validate(valid => {
        if (valid) {
          this.dataForm.organizeId= this.$store.getters.organizeId;

          if (!this.dataForm.id) {
            if (!this.pictureList.length)
              return this.$message.warning("新闻封面图片不能为空");
            request({
              url: `/api/news/Xy_ks_news`,
              method: "post",
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  (this.visible = false), this.$emit("refresh", true);
                }
              });
            });
          } else {
            if (!JSON.parse(this.pictureList).length)
              return this.$message.warning("新闻封面图片不能为空");
            request({
              url: "/api/news/Xy_ks_news/" + this.dataForm.id,
              method: "PUT",
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refresh", true);
                }
              });
            });
          }
        }
      });
    }
  }
};
</script>
